var V=new Vue({
    el:'#main',
    data:()=>({
        seldata:[],//下拉选择的数据
        selval:0,//下拉选择的值
        listdata:{
            footer:{
                num: 1,
                sum_CBM: 1,
                sum_CNY: 10,
                sum_CTN: 1,
                sum_KGS: 1
            }
        },
        modellistdata:{
            footer:{
                num: 1,
                sum_CBM: 1,
                sum_CNY: 10,
                sum_CTN: 1,
                sum_KGS: 1
            }
        },
        port:[],//港口
        portId:0,//港口id
        page:1,
        pageSize:10,
        date:[],//时间
        current:{},//当前时间
        is_edit:$('#fun').val(),
        batch:[],
        allid:[],
        contentLength:0
    }),
    methods:{
        formatUnit:formatUnit,
        getdata(is_change){
            $('.loding').show()
            this.batch=[]
            $('table input').prop('checked',false)
            $.ajax({
                url:'/admin/container/'+$('#fun').val(),
                data: {
                    page: this.page,
                    pageSize: this.pageSize,
                    date: this.current, // 年year 月month
                    lang: this.lang,    
                    port_id:this.portId,  // 1全部
                    container_id:this.selval,  // 货柜ID
                },
                type:'post',
                success:(data)=>{
                    data = JSON.parse(data)
                    console.log(data);
                    if(data.container.length > 0){
	                    this.seldata=data.container
                        if(this.is_edit!='loading' && is_change){
                            this.selval=this.seldata[0].id
                        }
                    }else{
                        change_mes_type('.alert',2)
                        $('.alert').show()
                        $('.alert p').html('*暂无货柜信息')
                    }

                    data.buy_po_pack.content.forEach(element => {
                        element.is_che=false
                        element.buy_po_pack.forEach(item=>{
                            item.maxval=item.qty
                            item.is_che=false
                        })
                    });

                    this.listdata=data.buy_po_pack
                    this.port=data.port
                    this.modellistdata=JSON.parse(JSON.stringify(data.buy_po_pack))
                    if(data.buy_po_pack.packNum)
                    this.Resetnav(this.page,data.buy_po_pack.packNum)

                    // this.contentLength = this.listdata.content.length
                    this.contentLength = data.buy_po_pack.content.length
                    $('.loding').hide()
                }
            })
        },
        Resetnav(pageCurrent, pageSum) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.pageSize;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator('#pagination', {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all:'<li class="page"><a href="javascript:;">'+myPageCount+' / '+this.page+'PG , '+this.pageSize+' /PG</a></li>',
                onPageChange:(num, type)=>{
                    if (type == "change") {
                        this.page=num
                        this.getdata()
                    }
                }
            });
        },
        changedate(index,is_change){
            this.current=this.date[index]
            if(!is_change)
            {
                if (index >= 0) {
                    this.date[0] = JSON.parse(JSON.stringify(this.date[index]))
                    this.current = JSON.parse(JSON.stringify(this.date[0]))
                    var time = JSON.parse(JSON.stringify(this.date[0]))
                } else {
                    var time = JSON.parse(JSON.stringify(this.date[0]))
                    if (time.month + 1 == 13) {
                        time.year++
                        time.splyear++
                        time.month = 1
                    } else {
                        time.month++
                    }
                    this.date[0] = JSON.parse(JSON.stringify(time))
                    this.current = JSON.parse(JSON.stringify(this.date[0]))
                }
                for (var i = 1; i < 6; i++) {
                    if (time.month + 1 == 13) {
                        time.year++
                        time.splyear++
                        time.month = 1
                    } else {
                        time.month++
                    }
                    this.date.splice(i, 1, JSON.parse(JSON.stringify(time)))
                }
            }
            this.getdata(true)
        },
        allsel(event,dataname){
            if ($(event.target).prop('checked')) {
                this[dataname].content.forEach(element=>{
                    element.is_che=true
                    element.buy_po_pack.forEach(item=>{
                        item.is_che=true
                    })
                })
            } else {
                this[dataname].content.forEach(element=>{
                    element.is_che=false
                    element.buy_po_pack.forEach(item=>{
                        item.is_che=false
                    })
                })
            }
        },
        onesel(event,index,dataname,is_p){
            if(is_p){
                if(index != undefined){
                    if($(event.target).prop('checked')){
                        this[dataname].content[index].buy_po_pack.forEach(element=>{
                            element.is_che=true
                        })
                    }else{
                        this[dataname].content[index].buy_po_pack.forEach(element=>{
                            element.is_che=false
                        })
                    }
                }
            }else{
                if(this[dataname].content[index].buy_po_pack.every(element=>{
                    return element.is_che
                })){
                    this[dataname].content[index].is_che=true
                }else{
                    this[dataname].content[index].is_che=false
                }
                
            }
            if($(event.target).prop('checked')){
                if(this[dataname].content.every(element=>{
                    if(element.is_che==false)
                    return false
                    return element.buy_po_pack.every(item=>{
                        return item.is_che
                    })
                })){
                    $(event.target).parents('table').find("th input").prop('checked',true)
                }
            }else{
                $(event.target).parents('table').find("th input").prop('checked',false)
            }
        },
        save(url){
            if(url == 'toContainer'){
                var content = this.modellistdata
            }else{
                var content = this.listdata
            }
            $.ajax({
                url:'/admin/container/'+url,
                data: {
                    content,
                    container_id: this.selval,
                },
                type:'post',
                success:(data)=>{
                    data = JSON.parse(data)
                    console.log(data)
                    if(data.status==1){
                        this.getdata(false)
                    }
                    change_mes_type('.alert',data.status)
                    $('.alert').show()
                    $('.alert p').html('*'+data.msg)
                }
            })
        },
        changeContainer(){
            if(this.is_edit == 'adjust'){
                this.getdata(false)
            }
        },
        changePort(id){
            this.portId=id
            this.getdata(true)
        },
        changeVal(dataname,index,key){
            if(this[dataname].content[index].buy_po_pack[key].qty>this[dataname].content[index].buy_po_pack[key].maxval){
                this[dataname].content[index].buy_po_pack[key].qty=this[dataname].content[index].buy_po_pack[key].maxval
            }
        },
        Money(value, num) {
            num = num > 0 && num <= 20 ? num : 2;
            value = parseFloat((value + "").replace(/[^\d\.-]/g, "")).toFixed(num) + ""; //将金额转成比如 123.45的字符串
            var valueArr = value.split(".")[0].split("") //将字符串的数变成数组
            const valueFloat = value.split(".")[1]; // 取到 小数点后的值
            let valueString = "";
            for (let i = 0; i < valueArr.length; i++) {
            valueString += valueArr[i] + ((i + 1) % 3 == 0 && (i + 1) != valueArr.length ? "," : ""); //循环 取数值并在每三位加个','
            }
            const money = valueString.split("").join("") + "." + valueFloat; //拼接上小数位
            return money
        }
    },
    mounted(){
        var date = new Date()
        var time = {
            year: date.getFullYear(),
            splyear: date.getFullYear().toString().slice(2, 4) - 0,
            month: date.getMonth() + 1
        }
        this.date[0] = JSON.parse(JSON.stringify(time))
        this.changedate(0,false)
        this.getdata(true)
        $("#toContainer_editModal").on("hidden.bs.modal", function(){
            location.reload()
        });
    }
})